package cn.hnh.utils;

import cn.hnh.vo.ArchivesArticleVo;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author HuangNianHong
 * @Time 2020/12/22 11:35
 * 工具类
 * 将List中的元素按年份相同的分配到一起
 */
public class ListToMapByYear {

    public static synchronized Map<String, List<ArchivesArticleVo>> transfer(List<ArchivesArticleVo> list){
        Map<String,List<ArchivesArticleVo>> map=new HashMap<>();
        for (ArchivesArticleVo archivesArticleVo : list) {
            String year = DateFormatUtils.formatYear(archivesArticleVo.getTime());
            String date =  DateFormatUtils.formatDate(archivesArticleVo.getTime());
            archivesArticleVo.setDate(date);
            List<ArchivesArticleVo> l = map.get(year);
            if(l==null){
                l=new ArrayList<>();
                l.add(archivesArticleVo);
            }else{
                l.add(archivesArticleVo);
            }
            map.put(year, l);
        }
        return map;
    }

}
